package com.KA.service.impl;

import com.KA.dto.ConsumeVo;
import com.KA.dto.InsertMoneyVo;
import com.KA.dto.RefundVo;
import com.KA.dto.checkWalletVo;
import com.KA.entity.UserWallet;
import com.KA.mapper.UserWalletMapper;
import com.KA.service.UserWalletService;
import com.KA.utils.UserThreadLocal;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class UserWalletServiceImpl extends ServiceImpl<UserWalletMapper, UserWallet> implements UserWalletService {


    @Override
    public BigDecimal checkWallet(checkWalletVo checkWalletVo) {

        //获得当前登录用户id
        String userId = UserThreadLocal.get().getId();

        //判断钱包状态，是否正常
        if(checkWalletVo.getStatus()!=1) {
           throw  new RuntimeException("钱包异常");
        }
        UserWallet userWallet = baseMapper.selectOne(new LambdaQueryWrapper<UserWallet>()
                .eq(UserWallet::getUId, userId)
                .eq(UserWallet::getMobile, checkWalletVo.getMobile())
                .eq(UserWallet::getIdCard, checkWalletVo.getIdCard()));

        return userWallet.getMoney();
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public Boolean consume(ConsumeVo consumeVo) {
        //获得当前登录用户id
        String userId = UserThreadLocal.get().getId();
        //钱包信息
        UserWallet userWallet = baseMapper.selectOne(new LambdaQueryWrapper<UserWallet>()
                .eq(UserWallet::getUId,userId)
                .eq(UserWallet::getId, consumeVo.getUserWalletId()));

        //支付密码是否正确
        //consumeVo.getPaymentCode()加密
        String PaymentCode=consumeVo.getPaymentCode();

        if(!userWallet.getPaymentCode().equals(PaymentCode)){
            throw new RuntimeException("密码错误");
        }

        //判断钱包状态，是否正常
        if(userWallet.getStatus()!=1) {
            throw  new RuntimeException("钱包异常");
        }


       //查看钱包余额是否足够
       if(userWallet.getMoney().subtract(consumeVo.getPayMoney()).compareTo(new BigDecimal(0.00))==-1){
           return false;
       }

        BigDecimal subtract = userWallet.getMoney().subtract(consumeVo.getPayMoney());
        UserWallet userWallet1 = new UserWallet();
        userWallet1.setId(consumeVo.getUserWalletId());
        userWallet1.setMoney(subtract);

        int result = baseMapper.consume(userWallet.getId(), consumeVo.getPayMoney());
        if (result != 1) {
            throw new RuntimeException("余额不足");
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public Boolean refund(RefundVo refundVo) {

        //钱包信息
        UserWallet userWallet = baseMapper.selectOne(new LambdaQueryWrapper<UserWallet>()
                                                      .eq(UserWallet::getId, refundVo.getUserWalletId()));
        //判断钱包状态，是否正常
        if(userWallet.getStatus()!=1) {
            throw  new RuntimeException("钱包异常");
        }

        int refund = baseMapper.refund(refundVo.getUserWalletId(), refundVo.getRefundMoney(),userWallet.getMoney());

        return refund>1;
    }

    @Override
    public Boolean InsertMoney(InsertMoneyVo insertMoneyVo) {

        //获得当前登录用户id
        String userId = UserThreadLocal.get().getId();
        //钱包信息
        UserWallet userWallet = baseMapper.selectOne(new LambdaQueryWrapper<UserWallet>()
                .eq(UserWallet::getUId, userId)
                .eq(UserWallet::getStatus,insertMoneyVo.getStatus())
                .eq(UserWallet::getMobile,insertMoneyVo.getMobile()));

        int refund = baseMapper.IsertMoney(userId,insertMoneyVo.getMobile(), insertMoneyVo.getMoney(),userWallet.getMoney());
        return refund>1;
    }


}
